<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<%@ include file="../common.jsp"%>
<%@ page import="org.springside.core.Constants,cn.firstsoft.firstframe.admin.*"%>
<%@ page import="java.io.*,org.jdom.*,org.jdom.input.*,org.jdom.output.*,java.util.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%
	//为0是正常的
	int error_code = 0;
	String id = request.getParameter("id");
	if (id != null && !"".equals(id)) {

		String datacenterXmlFile = System.getProperty("oa_application.config") + File.separator + "datacenter.xml";
		File datacenterFile = new File(datacenterXmlFile);
		
		//校验，合法性检查，根据安装类型和应用服务器类型判断
		String xmlFile = "";
		if ("TOMCAT".equals(Environment.getWebServerName())) { 
			xmlFile = Environment.getWebServerPath() + File.separator + "conf" + File.separator + "server.xml";
		} else if ("APUSIC".equals(Environment.getWebServerName())) {
			xmlFile = Environment.getWebServerPath() + File.separator + "config" + File.separator + "datasources.xml";
		} else {
			xmlFile = "unknown";
		}
		File file = new File(xmlFile);
		
		if (file.exists() && datacenterFile.exists()) {
			SAXBuilder builder = new SAXBuilder();
			Document doc;
			Document dcdoc;
			try {
				dcdoc = builder.build(datacenterFile);
				Element datacenters = dcdoc.getRootElement();
				
				List datacenterList = datacenters.getChildren("datacenter");
				
				for (int i=0; i<datacenterList.size(); i++) {
					Element datacenter = (Element)datacenterList.get(i);
					
					if (datacenter.getAttributeValue("id").equals(id)) {
						datacenters.removeContent(datacenter);
						break;
					}
				}			
				XMLOutputter outp1 = new XMLOutputter();
				Format format = Format.getPrettyFormat();
				format.setEncoding("UTF-8");
				outp1.setFormat(format);
				outp1.output(dcdoc, new FileOutputStream(datacenterFile));					
				
				if ("TOMCAT".equals(Environment.getWebServerName())) { 
					doc = builder.build(file);
					Element server = doc.getRootElement();
					Element service = server.getChild("Service");
					Element engine = service.getChild("Engine");
					Element host = engine.getChild("Host");
					Element context = host.getChild("Context");
					List resourceList = context.getChildren("Resource");
				
					for (int i=0; i<resourceList.size(); i++) {
						Element resource = (Element)resourceList.get(i);			
						if (resource.getAttributeValue("name").equals("jdbc/"+id)) {
							context.removeContent(resource);
							break;
						}
					}
					XMLOutputter outp = new XMLOutputter();
					outp.setFormat(Format.getPrettyFormat());
					outp.output(doc, new FileOutputStream(file));
				} else if ("APUSIC".equals(Environment.getWebServerName())) {
					doc = builder.build(file);
					Element datasources = doc.getRootElement();
					List resourceList = datasources.getChildren("datasource");	
					for (int i=0; i<resourceList.size(); i++) {
						Element resource = (Element)resourceList.get(i);			
						if (resource.getAttributeValue("jndi-name").equals("jdbc/"+id)) {
							datasources.removeContent(resource);
							break;
						}
					}									
					XMLOutputter outp = new XMLOutputter();
					outp.setFormat(Format.getPrettyFormat());
					outp.output(doc, new FileOutputStream(file));
				}
				
			} catch (JDOMException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			error_code = 1;
		}
	} else {
		error_code = 2;
	}
%>
<html>
<head>
<title>管理控制台 - 数据中心</title>
<%@ include file="/common/meta2.jsp"%>
<link href="../style.css" type="text/css" rel=stylesheet>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%">
  <tr>
  	<td width="100%">
		<% if (error_code > 0) { %>
		反注册数据中心失败！错误编号：<%=error_code %>，<a href="index.jsp">点这里返回数据中心列表</a>
		<% } else { %>
  		反注册数据中心完成！需要重新启动服务器！<a href="index.jsp">点这里返回数据中心列表</a>
  		<% } %>
  	</td>
  </tr>
</table>
</body>
</html>
